import random
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt
from ..widgets.mixed_button import MixedButton
from ..widgets.tart_frame import TartFrame
from ..widgets.tart_table import TartTable
from ..widgets.lift_plate import LiftPlate
from ..data import game_data
from . import screen_manager
from ..widgets.job_menu import JobMenu

class TartManagement(QtGui.QWidget):
	def __init__(self, *args, **kw):
		QtGui.QWidget.__init__(self, *args, **kw)
		self.setGeometry(0, 0, 1024, 768)
#
#		make the child widgets
#
		self.make_widgets()
#
#		make the sliding menu and the do-dad to animate it
#
		self.make_menu()
		self.make_shortcuts()
#
#		we'll use this to keep track of the job slot being
#		edited if the menu is open
#
		self.editing = None
#
#		screen should start hidden
#
		self.hide()


#       kudos to rudistoned for the code
#       I like the trick of using shorthand variables for classes
#
	def make_shortcuts(self):
		Seq = QtGui.QKeySequence
		Shorty = QtGui.QShortcut
		Shorty(Seq(Qt.Key_Escape), self, self.remove_menu)

	def remove_menu(self):
		if self.menu_visible:
			pass
		else:
			return
		self.editing.blanked(False)
		self.editing = None

		r0 = self.menu.geometry()
		x0 = r0.x()
		y0 = r0.y()

		whoop = random.randint(1,3)
		if whoop == 1:
			x1 = -100
			y1 = -100
		elif whoop == 2:
			x1 = 1100
			y1 = -100
		elif whoop == 3:
			x1 = x0
			y1 = -1000
		else:
			raise(Exception(
				"unexpected whoop: {}".format(whoop)
			))
#
#		make a new property animator
#
		self.pa = self.make_pa(
			x0 = x0,
			y0 = y0,
			x1 = x1,
			y1 = y1
		)
		self.pa.start()
		self.menu_visible = False

	def make_menu(self):
#
#		make the menu object. this is going to be a collection
#		of paper signs on a pair of brass rails that slides in from 
#		off-screen
#
		self.menu = JobMenu(self)
#
#		make sure it starts off-screen
#
		self.menu.move(-100, -100)
		self.menu.clicked.connect(self.menu_clicked)
		self.menu.banish.connect(self.remove_menu)

	def menu_clicked(self, item):
		self.editing.set_text(item.text())
		self.editing.blanked(False)

	def make_pa(self, x0=-100, y0 = -100, x1 = 600, y1 = -100):
		pa = QtCore.QPropertyAnimation(self.menu, "geometry")
		pa.setDuration(500)
		pa.setStartValue(QtCore.QRect(x0, y0, 101, 642))
		pa.setEndValue(QtCore.QRect(x1, y1, 101, 642))
		pa.setEasingCurve(QtCore.QEasingCurve.OutBack)
		return pa


#
#	make the top-level widgets for the screen
#
	def make_widgets(self):
#
#		title - bump mapped gold, Gabrielle fonts
#		Says "Tarts" although I'm leaning toward 
#		"Tart Management", left justifying it
#		and using the RHS space for widgetry
#
		title = QtGui.QLabel(self)
		title.setGeometry((1024-120)/2, 20, 120, 55)
		path = "resources/tart_title.png"
		title.setPixmap(QtGui.QPixmap(path))
#
#		frame for the picture and description
#
		image = TartFrame(self)
		image.move(790, 87)
		self.image = image;
#
#		"elevator buttons" style up and down buttons
#		to scroll the table
#
#		create it, place it, wire up the buttons
#
                self.lift_plate = LiftPlate(self)
                self.lift_plate.move(794, 663)
		self.lift_plate.set_cbs(self.go_up, self.go_down)
#
#		button to get detail on the current girl
#
		self.detail_b = MixedButton("Detail", self)
		self.detail_b.setGeometry(30, 700, 210, 50)
#
#		done button takes us back to the main screen
#		(I keep wondering about building an overlay
#		window architcture like the original WM. Then 
#		"Back" would make sense...)
#
		self.done_b = MixedButton("Done", self)
		self.done_b.setGeometry(535, 700, 210, 50)
		self.done_b.clicked.connect(self.to_main)
#
#		and the tart table itself!
#
		self.table = TartTable(self)
		self.table.move(5,90)
		self.table.new_selection.connect(self.update_frame)
		self.table.job_box_clicked.connect(self.job_click)
		self.menu_visible = False

	def to_main(self):
		screen_manager.show("main screen")

	def job_click(self, row, box):
		if row.girl == None:
			return
		flag = box.toggle_blank()

		if flag:
			if self.editing != None:
				self.editing.blanked(False)
			self.move_menu(box)
			self.editing = box
		else:
			self.remove_menu()
			self.editing = None

	def move_menu(self, box):
#
#		where we stop should depend on the cell clicked
#
		idx = box.period_index
		if idx == 0:
			x1 = 260
		elif idx == 1:
			x1 = 350
		elif idx == 2:
			x1 = 440
		elif idx == 3:
			x1 = 530
		else:
			raise Exception("unknown job index {}".format(idx))
		y1 = -100

#
#		three options for the side from which the menu enters
#
		which = random.randint(1,3)
#
#		but first, we should consider if the menu is already 
#		present
#
		if self.menu_visible:
			r = self.menu.geometry()
			x0 = r.x()
			y0 = r.y()
#
#		otherwise we slide in from the top or from the side
#		chosen at random
#
		elif which == 1:
			x0 = -100
			y0 = -100
		elif which == 2:
			x0 = 800
			y0 = -100
		elif which == 3:
			x0 = x1
			y0 = -1000
		else:
			raise(Exception(
				"logic error: unexpected randint {}".format(
					which
				)
			))

		self.pa = self.make_pa(x0 = x0, y0 = y0, x1 = x1, y1 = y1)
		self.pa.start()
		self.menu_visible = True

	def go_up(self):
		self.table.up_key()

	def go_down(self):
		self.table.down_key()

	def set_image(self, path):
		self.image.set_image(path)

	def set_text(self, text):
		self.image.set_text(text)

	def showEvent(self, event):
		self.populate()
		self.table.highlight(0)
		girl = self.girl_at(0)
		self.update_frame(girl)
		super(TartManagement, self).showEvent(event)

	def update_frame(self, girl):
		self.set_image(girl.pics.random_profile())
		self.set_text(girl.desc)

	def girl_at(self, index):
		brothel = game_data.data.current_brothel()
		girls = brothel.girls
		n = len(girls)
		if n == 0 or index >= n:
			return None
		return girls[index]

	def populate(self):
		last = -1
		brothel = game_data.data.current_brothel()
		self.table.set_girls(brothel.girls)

#
#	pick up key pressed events
#
#	this belongs at app level, really
#	but this'll do for testing
#
	def keyPressEvent(self,e):
		n = -1
#
#		need to think about how the number keys
#		should work with a more flexible approach
#		to brothels
#
#		am I going to have a more flexible approach?
#		I think i'd like to pick up some of the room
#		management ideas from the end of my time on WM
#
#		anyway, until I know, it makes little sense to 
#		hotkey brothels. hell, until I let players buy more
#		than one, it makes little sense.
#
		k = e.key()
		if k == ord("M"):
			e.accept()
			screen_manager.show("main screen")
			return
		if k == ord("O"):
			e.accept()
			screen_manager.show("town screen")
			return
		if k == ord("S"):
			e.accept()
			screen_manager.show("slave market screen")
			return
		if k == Qt.Key_Down:
			e.accept()
			self.go_down()
			return
		if k == Qt.Key_Up:
			e.accept()
			self.go_up()
			return
		e.ignore()


if __name__ == "__main__":
	import sys
	from PyQt4.QtGui import QApplication
	app = QtGui.QApplication(sys.argv)

	font_db = QtGui.QFontDatabase()
	font_db.addApplicationFont("resources/Gabrielle.ttf")

	pic = QtGui.QLabel()
	pic.setGeometry(0, 0, 1024, 768)
	path = "resources/wood.jpg"
	pic.setPixmap(QtGui.QPixmap(path))

	class Girly:
		def __init__(self):
			self.jobs = []
		@property
		def morning(self):	return self.jobs[0]
		@property
		def afternoon(self):	return self.jobs[1]
		@property
		def evening(self):	return self.jobs[2]
		@property
		def night(self):	return self.jobs[3]

	app = QtGui.QApplication(sys.argv)

	screen = TartManagement(pic)
	screen.add_row([
		"Lillian", "A+", "A+", "A+",
		"Whoring", "Whoring", "Whoring", "Whoring"
	])
	screen.add_row([
		"Jennifer", "A", "A", "A",
		"Streets", "Streets", "Streets", "Streets"
	])
	screen.set_image('resources/characters/Sexy Girl/Profile9.jpg')
	screen.set_text("Everything about her makes men just want to fuck her.\n\nShe finds you mean and scary")

	happy_str = [
		#"100", "100", "100", "100", "100",
		#"Ecstatic", "Cheerful", "Sober", "Glum", "Depressed",
		"A-", "B+", "B", "B-", "C"
	]

	fatigue_str = [
		#"100", "100", "100", "100", "100",
		#"Energetic", "Active", "Flagging", "Tired", "Exhausted",
		"A-", "B+", "B", "B-", "C"
	]

	health_str = [
		#"100", "100", "100", "100", "100",
		#"Vigorous", "Fit", "Healthy", "Poorly", "Ill",
		"A-", "B+", "B", "B-", "C"
	]

	for i in range(5):
		screen.add_row([
			"clone", health_str[i], happy_str[i], fatigue_str[i],
			"streets", "streets", "streets", "streets"
		])

	pic.show()
	sys.exit(app.exec_())





